import { defineStore } from 'pinia';
import service from '/@/utils/request';
import { query } from '/@/utils/params';
import { Account } from '/@/views/system/account/account';
import { easyDate } from '/@/utils/formatTime';

export interface OrderVo {
	id?: string;
	name: string; // 公司名称
	tel: string; // 公司电话
	contactName: string; // 联系人
	contactTel: string; // 联系电话
	address: string; // 地址
	account?: string; // 账号
	password?: string; // 密码
}

export interface OrderDTO {
	name: string; // 公司名称
	tel: string; // 公司电话
	contactName: string; // 联系人
	contactTel: string; // 联系电话
	address: string; // 地址
	account: Account; // 账号
	applyer?: any;
	device?: any;
	password: string; // 密码
	createdAt: string;
}
export const useOrder = defineStore('orderStore', {
	actions: {
		async getOrderList(name: string, pageSize: number, pageNumber: number) {
			return service.get(query('/api/order', { name, pageSize, pageNumber })).then((res) => {
				res.list = res.list.map((c: OrderDTO) => {
					return {
						...c,
						accountName: c.applyer?.name,
						devicecode: c.device?.code,
						createdAt: easyDate(c.createdAt),
					};
				});
				return res;
			});
		},
		/**
		 * 新增
		 * @param vo
		 * @returns
		 */
		async saveOrder(vo: OrderVo) {
			return service.post('/api/order/create', vo);
		},

		/**
		 * 编辑
		 */
		async updateOrder(vo: OrderVo) {
			return service.patch(`/api/order/${vo.id}`, vo);
		},

		async delete(id: string) {
			return service.delete(`/api/order/${id}`);
		},
		async toggoleStatus(id: string, status: number) {
			return service.patch(`/api/order/${id}/status?status=${status}`);
		},
	},
});
